<?php
require_once(dirname(dirname(__FILE__)) . '/libs/db.class.php');
class Product
{
    public $db;
    function __construct()
    {
        $this->db = new DB();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        商品列表
    */
    function list()
    {
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        if (isset($_GET['cid'])) {
            $cid = $_GET['cid'];
        }
        $output = ['errcode' => 0, 'msg' => 'success'];
        $sql = "select id,name,cover,price,description,market_price,brand,category,sales_volume from products";
        if ($cid) {
            $sql .= " where cid={$cid}";
        }
        $sql .= " limit {$start},{$page_num}";
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 获取总记录数
            $sql = "select count(id) as count from products";
            if ($cid) {
                $sql .= " where cid={$cid}";
            }
            $res = $this->db->get_one($sql);
            $total = $res['count'];
            $output['data'] = ['list' => $list, 'total' => $total];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取商品失败';
        }

        echo json_encode($output);
    }


    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($id = $_GET['id']) {
            $sql = "select * from products where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['colors'] = explode('|', $res['colors']);
                $res['versions'] = explode('|', $res['versions']);
                $res['imgs'] = explode('|', $res['imgs']);
                $output['data'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '商品不存在';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = 'params error';
        }
        echo json_encode($output);
    }



    // 商品推荐
    function recom_list()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($cid = $_GET['cid']) {
            $sql = "select id,name,cover,price,description,market_price,brand,category,sales_volume from products where cid in (1,2,3,4,5,6,7,8) and cid<>{$cid} limit 5";
            $list = $this->db->query($sql);
            if ($list) {
                $output['data'] = $list;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '暂无推荐';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    // 商品搜索
    function search()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($keywords = $_GET['keywords']) {
            $sql = "select id,name,cover,price,market_price,description,brand,category,sales_volume from products where name like '%{$keywords}%'";
            $list = $this->db->query($sql);
            if ($list) {
                $output['data'] = $list;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '暂无搜索结果';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
}
